import { NextFunction, Request, Response } from "express";
import VenueService from "../services/VenueService";
import ResponseDTO from "@/dto/ResponseDTO";
import CourtService from "@/services/CourtService";

class VenueController {
  /** 创建场馆 */
  public async createVenue(req: Request, res: Response, next: NextFunction) {
    try {
      const venueParams = req.body;
      await VenueService.createVenue(venueParams);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 获取场馆列表 */
  public async getVenueList(req: Request, res: Response, next: NextFunction) {
    try {
      const listParams = {
        pageSize: Number(req.query?.pageSize || 10),
        pageIndex: Number(req.query?.pageIndex || 1),
        venueName: String(req.query?.venueName || ""),
        address: String(req.query?.address || ""),
      };
      const venueList = await VenueService.getVenueList(listParams);
      res.locals.data = venueList;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 根据场馆名称获取场馆列表 */
  public async getVenueSelectListByVenueName(
    req: Request,
    res: Response,
    next: NextFunction
  ) {
    try {
      const listParams = {
        pageSize: Number(req.query?.pageSize || 10),
        pageIndex: Number(req.query?.pageIndex || 1),
        venueName: String(req.query?.venueName || ""),
      };
      const data = await VenueService.getVenueList(listParams);
      const list = data.list.map((item) => {
        return {
          value: item.venueId,
          label: item.venueName,
        };
      });
      res.locals.data = { ...data, list };
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 更新场馆信息 */
  public async updateVenue(req: Request, res: Response, next: NextFunction) {
    try {
      const venueParams = req.body;
      if (!venueParams?.venueId) {
        res.send(ResponseDTO.error(100, "参数错误，venueId不能为空"));
        return;
      }
      await VenueService.updateVenue(venueParams);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 删除场馆信息 */
  public async deleteVenue(req: Request, res: Response, next: NextFunction) {
    try {
      const venueParams = req.body;
      if (!venueParams?.venueId) {
        res.send(ResponseDTO.error(100, "参数错误，venueId不能为空"));
        return;
      }
      await VenueService.deleteVenue(venueParams.venueId as string);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 获取场馆信息 */
  public async getVenueById(req: Request, res: Response, next: NextFunction) {
    try {
      const venueId = (req.query?.venueId as string) || "";
      const venue = await VenueService.getVenueById(venueId);
      const courts = await CourtService.getCourtList({ venueId });
      const courtsData = courts.list.map((ele: { courtId: any; courtName: any; }) => ({
        courtId: ele.courtId,
        courtName: ele.courtName,
      }));
      res.locals.data = { ...venue, courts: courtsData };
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }
}

export default new VenueController();
